package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //拷贝数据 shoppingCartDTO --> shoppingCart
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //传入当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        log.info("添加的购物车数据为：{}",shoppingCart);

        //判断传入的信息是否存在
        if(shoppingCartMapper.getShoppingCart(shoppingCart) != null){
            //如果菜品存在且口味不一致，将菜品的数量加1，然后修改购物车
            shoppingCartMapper.addShoppingCart(shoppingCart);
        }else {
            //查询套餐或者菜品id
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();

            if(dishId != null){ //判断是否是菜品
                //根据菜品id查询购物车对应的数据
                ShoppingCart dish = shoppingCartMapper.getByDishId(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getAmount());
            }else if(setmealId != null){//判断是否是套餐
                //根据套餐id查询购物车对应的数据
                ShoppingCart Setmeal = shoppingCartMapper.getBySetmealId(setmealId);
                shoppingCart.setName(Setmeal.getName());
                shoppingCart.setImage(Setmeal.getImage());
                shoppingCart.setAmount(Setmeal.getAmount());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //新增菜品
            shoppingCartMapper.save(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> getByUserId(Long userId) {
        return shoppingCartMapper.getByUserId(userId);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCart
     */
    @Override
    public void sub(ShoppingCart shoppingCart) {

        if(shoppingCartMapper.getShoppingCart(shoppingCart).getNumber() > 1 ){
            //如果数量大于1，则减1
            shoppingCartMapper.subShoppingCart(shoppingCart);
        }else if(shoppingCartMapper.getShoppingCart(shoppingCart).getNumber() == 1 ){
            //如果数量等于1，则删除该菜品
            shoppingCartMapper.delShoppingCart(shoppingCart);
        }


//        //判断是否是菜品
//        if(dishId != null){
//            ShoppingCart byDish = shoppingCartMapper.getShoppingCart(shoppingCart);
//            log.info("购物车的菜品信息为：{}",byDish);
//            if(byDish.getNumber() > 1 ){//如果数量大于1，则减1
//                shoppingCartMapper.subDish(byDish);
//                log.info("菜品数量为：{}",byDish.getNumber()+"未删除");
//            }else if(byDish.getNumber() == 1 ){//如果数量等于1，则删除该菜品
//                shoppingCartMapper.delDish(byDish);
//                log.info("菜品数量为：{}",byDish.getNumber()+"已经删除");
//            }
//
//        }
//        if(setmealId != null){
//            ShoppingCart bySetmeal = shoppingCartMapper.getSetmealNumber(shoppingCart);
//            log.info("购物车的套餐信息为：{}",bySetmeal);
//            if(bySetmeal.getNumber() > 1){//如果数量大于1，则减1
//                shoppingCartMapper.subSetmeal(bySetmeal);
//            }else if(bySetmeal.getNumber() == 1){//如果数量等于1，则删除该套餐
//                shoppingCartMapper.delSetmeal(bySetmeal);
//            }
//
//        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean(Long userId) {
        shoppingCartMapper.clean(userId);
    }
}
